package com.gzc.just.play.last.war.centralserver.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Server Status Service
 * Monitors and reports server status and statistics
 */
@Service("central-serverStatusService")
public class ServerStatusService {
    private static final Logger logger = LoggerFactory.getLogger(ServerStatusService.class);
    
    // Server statistics
    private long startTime;
    private int totalConnections;
    private int activeConnections;
    private final Map<String, Object> serverMetrics = new HashMap<>();
    
    /**
     * Initialize server status tracking
     */
    public void initialize() {
        startTime = System.currentTimeMillis();
        logger.info("Server status service initialized");
    }
    
    /**
     * Update connection metrics
     */
    public void updateConnectionMetrics(int total, int active) {
        this.totalConnections = total;
        this.activeConnections = active;
        
        logger.debug("Connection metrics updated: total={}, active={}", total, active);
    }
    
    /**
     * Record server metric
     */
    public void recordMetric(String metric, Object value) {
        serverMetrics.put(metric, value);
        logger.debug("Metric recorded: {}={}", metric, value);
    }
    
    /**
     * Get server uptime in milliseconds
     */
    public long getUptime() {
        return System.currentTimeMillis() - startTime;
    }
    
    /**
     * Get server statistics
     */
    public Map<String, Object> getServerStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("uptime", getUptime());
        stats.put("totalConnections", totalConnections);
        stats.put("activeConnections", activeConnections);
        stats.putAll(serverMetrics);
        
        return stats;
    }
    
    /**
     * Get server status
     */
    public Map<String, Object> getServerStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("serverId", "central-server-01");
        status.put("status", "online");
        status.put("uptime", getUptime());
        status.put("totalConnections", totalConnections);
        status.put("activeConnections", activeConnections);
        status.putAll(serverMetrics);
        
        return status;
    }
}